package com.controller;

import com.dto.HttpClientMapResult;
import com.service.iml.UserService;
import com.utils.JwtTokenUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private UserService userService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @GetMapping ("/token")
    public Map token() {
        return new HttpClientMapResult(HttpURLConnection.HTTP_OK).putMsg("成功利用 Token 访问接口");
    }

    @GetMapping("/download/avatar")
    public void downloadAvatar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        byte[] avatar = userService.getUser("oXm676zkG_cyjvJmcX-3JgdqRqi0").getAvatar();
        response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("头像.png", "UTF-8"));
        response.setContentType("image/png"); //必须设置内容类型
        OutputStream out = response.getOutputStream();
        out.write(avatar);
        out.flush();
        out.close();
    }

}
